
public class LinkedList<E> {
    //节点
    private class Node {
        private E e; //节点元素
        private Node next; // 指向下一个节点的指针

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private int size; //链表中的元素个数
    private Node dummyHead; //头结点指针

    public LinkedList() {
        dummyHead = new Node(null, null);
        size = 0;
    }

    /**
     * 获取头节点
     *
     * @return
     */
    public Node getHead() {
        return dummyHead.next;
    }

    /**
     * 获取链表元素个数
     *
     * @return
     */
    public int getSize() {
        return size;
    }

    /**
     * 判断链表是否为空，为空返回true，否则返回false
     *
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 在链表index位置插入元素e
     *
     * @param index
     * @param e
     */
    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add failed. Require index >= 0 && index =< size");
        }
        add(dummyHead, index, e);
        //Node prev = dummyHead;
        //for (int i = 0; i < index; i++) {
        //    prev = prev.next;
        //}
        //prev.next = new Node(e, prev.next);
        //size++;
    }

    private void add(Node node, int index, E e) {
        if (index == 0) {
            // TODO 插入元素
            node.next = new Node(e, node.next);
            size++;
            return;
        }
        index--;
        add(node.next, index, e);
    }


    /**
     * 在队列尾部添加元素e
     *
     * @param e
     */
    public void addLast(E e) {
        add(size, e);
    }

    /**
     * 在链表头部插入元素e
     *
     * @param e
     */
    public void addFirst(E e) {
        add(0, e);
    }

    /**
     * 获取链表中位置为index的元素
     *
     * @param index
     */
    public E get(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add failed. Index illegal.");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.e;
    }

    public E getFirst() {
        return get(0);
    }

    public E getLast() {
        return get(size - 1);
    }

    /**
     * 判断链表是否包含元素e
     *
     * @param e
     * @return
     */
    public boolean contains(Node head, E e) {
        //带虚拟头节点的链表
        if (head == null) {
            return false;
        }
        boolean res = contains(head.next, e);
        return head.e == e ? true : res;
    }

    /**
     * 修改链表中下标为index的元素
     *
     * @param index
     * @return
     */
    public void set(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Set failed. Index illegal.");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.e = e;
    }

    /**
     * 删除链表中位置为 index 的元素
     *
     * @param index
     * @return
     */
    public E remove(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Remove failed. Index illegal.");
        }
        Node cur = dummyHead;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        Node delNode = cur.next;
        cur.next = delNode.next;
        delNode.next = null;
        size--;
        return delNode.e;
    }

    /**
     * 删除链表第一个元素
     *
     * @return
     */
    public E removeFirst() {
        return remove(0);
    }

    /**
     * 删除链表最后一个元素
     *
     * @return
     */
    public E removeLast() {
        return remove(size - 1);
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyHead;
        while (cur.next != null) {
            res.append(cur.next.e + "->");
            cur = cur.next;
        }
        res.append("Null");
        return res.toString();
    }
}
